package edu.njust.Service.stayregister.impl;

import edu.njust.Service.stayregister.StayRegisterService;
import edu.njust.dao.RoomMapper;
import edu.njust.dao.StayRegisterMapper;
import edu.njust.domain.Room;
import edu.njust.domain.StayRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PipedReader;
import java.util.*;

@Service
public class StayRegisterServiceImpl implements StayRegisterService {
    @Autowired
    private StayRegisterMapper stayRegisterMapper;
    @Autowired
    private RoomMapper roomMapper;


    @Override
    public boolean insertAll(StayRegister stayRegister) {
        return stayRegisterMapper.insertAll(stayRegister)>0;
    }

    @Override
    public int selectByRoom(int roomID) {
        return stayRegisterMapper.selectByRoom(roomID);
    }

    @Override
    public List<Integer> selectBypassengerID(int passengerID) {
        return stayRegisterMapper.selectByUser(passengerID);
    }

    @Override
    public List<StayRegister> selectMoney(int id) {
        return stayRegisterMapper.selectMoney(id);
    }

    @Override
    public List<StayRegister> selectMoneyBypassengerID(int passengerID) {
        List<Integer> list = selectBypassengerID(passengerID);
        List<StayRegister> result=new ArrayList<>();
        for (int i=0;i<list.size();i++){

            List<StayRegister> stayRegisters = selectMoney(list.get(i));
            result.addAll(stayRegisters);
        }
        return result;
    }

    @Override
    public double getAllMoneyNum(List<StayRegister> stayRegisters) {
        double moneyNum=0.0;
        Map<Integer,Integer> rooms=new HashMap<>();
        for (int i=0;i<stayRegisters.size();i++){
            rooms.put(stayRegisters.get(i).getRoomID(),stayRegisters.get(i).getStayNumber());
        }
        for (Integer key:rooms.keySet()){
            double standardPriceDay = roomMapper.selectById(key).getStandardPriceDay();
            moneyNum+=standardPriceDay*rooms.get(key);
        }
        //System.out.println(rooms);
        //已完成单独计算房间价格
        for (int i=0;i<stayRegisters.size();i++){
            moneyNum+=stayRegisters.get(i).getConsumptionMoney();
        }
        return moneyNum;
    }

    @Override
    public boolean updatebill(int id) {
        return stayRegisterMapper.updatebill(id)>0;
    }
}
